package com.example.login.activity

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.example.activity.BaseActivity
import com.example.common.MMKVUtils

import com.example.login.databinding.AcLoginBinding


import com.example.login.factory.ViewModelFactory
import com.example.login.model.LoginState
import com.example.login.viewmodel.LGViewModel
import com.kongzue.dialogx.dialogs.MessageDialog
import com.kongzue.dialogx.dialogs.TipDialog
import com.kongzue.dialogx.dialogs.WaitDialog
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

@Route(path = "/login/login")
class LoginActivity : BaseActivity() {

   // private lateinit var loginViewModel: LoginViewModel
    private lateinit var binding: AcLoginBinding
    private lateinit var lgVM: LGViewModel
    private lateinit var firstName:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = AcLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)

        firstName = MMKVUtils.getString("firstname","").toString();
        binding.etUsername.setText(firstName)
        binding.ivBack.setOnClickListener {
            finish()
        }
        readFile()
        binding.btLogin.setOnClickListener {
            val name : String = binding.etUsername.text.toString()
            val pw :String = binding.etPw.text.toString()
            if(name.isEmpty()|| pw.isEmpty()){
                MessageDialog.show("错误","用户名或密码不能为空","确定");
            }else{
//                Mapplication.getInstance().m_user.name = name
//                Mapplication.getInstance().m_user.ps = pw
                WaitDialog.show("登陆")
                lgVM.login(name,pw)
            }
        }
        binding.tvSignUp.setOnClickListener {
            ARouter.getInstance().build("/login/regist").navigation();
        }
        lgVM.loginResult.observe(this, Observer {
            if(it.type==LoginState._LoginState.LOGINSUCCESS){
                loginSuccesss()
            }else{
                TipDialog.show(it.msg.toString())
            }
       })
    }

    override fun onResume() {
        super.onResume()
    }



    fun loginSuccesss(){
        //获取intent对象
        ARouter.getInstance().build("/app/home").navigation();
        finish()
    }

    fun setLoginState(){
         lgVM = ViewModelProvider(this, ViewModelFactory())
            .get(LGViewModel::class.java)
    }

    private fun readFile(){
        setLoginState()
        val autologin: String? = MMKVUtils.getString("autologin","-1");
        if(autologin.equals("-1")){
            return
        }else{

        }
//        if(name !== "-1"){
//            val pw: String? = MMKVUtils.getString("pw","pw");
//            name?.let { pw?.let { it1 -> HttpMothod.login(it, it1, HttpResListense {
//                if(it.errorCode !=0){
//                    DialogXK.show(it.errorMsg)
//                    return@HttpResListense
//                }else{
//                    DialogXK.show("自动登录")
//                    lgVM.login(name,pw)
//                }
//            }) } }
//        }
    }


}